package com.ruoyi.APPMS.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.APPMS.domain.Warehouse;
import com.ruoyi.APPMS.service.IWarehouseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仓库管理Controller
 *
 * @author 谢雨琦
 * @date 2023-07-02
 */
@Api(tags="仓库管理控制器")
@RestController
@RequestMapping("/warehouse/warehouse")
public class WarehouseController extends BaseController
{
    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 查询仓库管理列表
     */
    @ApiOperation("查询仓库管理列表")
    @PreAuthorize("@ss.hasPermi('warehouse:warehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(Warehouse warehouse)
    {
        startPage();
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        if (warehouse.getWarehousename() != null) {
            queryWrapper.like("warehousename", warehouse.getWarehousename());
        }
        List<Warehouse> list = warehouseService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出仓库管理列表
     */
    @ApiOperation("导出仓库管理列表")
    @PreAuthorize("@ss.hasPermi('warehouse:warehouse:export')")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Warehouse warehouse)
    {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        if (warehouse.getWarehousename() != null) {
            queryWrapper.like("warehousename", warehouse.getWarehousename());
        }
        List<Warehouse> list = warehouseService.list(queryWrapper);
        ExcelUtil<Warehouse> util = new ExcelUtil<Warehouse>(Warehouse.class);
        util.exportExcel(response, list, "仓库管理数据");
    }

    /**
     * 获取仓库管理详细信息
     */
    @ApiOperation("获取仓库管理详细信息")
    @PreAuthorize("@ss.hasPermi('warehouse:warehouse:query')")
    @GetMapping(value = "/{warehouseid}")
    public AjaxResult getInfo(@PathVariable("warehouseid") Long warehouseid)
    {
        return success(warehouseService.getById(warehouseid));
    }

    /**
     * 新增仓库管理
     */
    @ApiOperation("新增仓库管理")
    @PreAuthorize("@ss.hasPermi('warehouse:warehouse:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Warehouse warehouse)
    {
        warehouse.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(warehouseService.save(warehouse));
    }

    /**
     * 修改仓库管理
     */
    @ApiOperation("修改仓库管理")
    @PreAuthorize("@ss.hasPermi('warehouse:warehouse:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Warehouse warehouse)
    {
        warehouse.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(warehouseService.updateById(warehouse));
    }

    /**
     * 删除仓库管理
     */
    @ApiOperation("删除仓库管理")
    @PreAuthorize("@ss.hasPermi('warehouse:warehouse:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{warehouseids}")
    public AjaxResult remove(@PathVariable List<Long> warehouseids)
    {
        return toAjax(warehouseService.removeByIds(warehouseids));
    }
}
